# encoding = UTF-8
'''
Created on 2018年2月8日
@author: wesley.wei

'''
#
import collections

# 定义变量  ID:0   FORENAME:1 ... 
ID,FORENAME,MIDDERNAME,SURNAME,DEPARTMENT = range(5)

# 声明一个元组
User = collections.namedtuple("User", "username forename middelname surname id")

# 存放用户名的set，可去重复
usernames = set()

# 保存数据的变量
users = {}

'''
主函数
'''
def main():
    for fileline in open('data/employee.txt', encoding='UTF-8'):
        fileline = fileline.rstrip() # 去除空格
        if fileline:
            fields = fileline.split(":")
            uname = generate_username(fields,usernames)    
            user = User(uname,fields[FORENAME],fields[MIDDERNAME],fields[SURNAME],fields[ID])
            users[fields[ID]]=user # 把user放入容器中，ID作为key
            
    print_user(users) # 打印输出        
            
'''
生成用户名，可完善程序，取第一个名字，中间名字，末尾名字首字母拼接 
如果存在，就加上后缀count
'''
def generate_username(fields,usernames):
    username = fields[FORENAME][0] + fields[MIDDERNAME][0]+fields[SURNAME][0]
    count = 1
    while username in usernames:
        username = "{0}{1}".format(username, count)
        count += 1
    usernames.add(username)
    return username

'''
打印输出
'''
def print_user(users):
    print("------------------------------用户信息-------------------------------")
    print("ID-----username-----forename-----middelname-----surname-----")
    
    for key in users:
        user = users[key]
        print( user.id + "     "  +user.username+"     "+user.forename+"     "+user.middelname+"     "+user.surname)
    

# 运行主函数
main();
